package com.example.demo.dao;

import com.example.demo.entity.GoodsItem;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import org.springframework.data.domain.Pageable;



public interface GoodsJPARepository extends JpaRepository<GoodsItem,Long>, JpaSpecificationExecutor<GoodsItem> {

    /*根据商品名称精确查询*/
    List<GoodsItem> findByName(String name);

    /*根据商品名称模糊查询*/
    List<GoodsItem> findByNameContaining(String name);

    /*根据商品价格范围查询*/
    List<GoodsItem> findByPriceBetween(Double price1,Double price2);

    /*根据商品名称和价格精确查询*/
    List<GoodsItem> findByNameAndPrice(String name,Double price);
    /**使用JPQL查询
     * 按照实体类名称、字段写查询条件
     */
    @Query("select g from Goods g where name = ?1")
    List<GoodsItem> findWithJPQLByName(String name);

    /**根据多个条件组合动态查询。参数为null时不做查询条件*/
    @Query("select g from Goods g where (?1 = null or name like %?1%) and (?2 = null or ?3 = null or (price between ?2 and ?3))")
    List<GoodsItem> findWithJPQLByNameAndPriceBetween(String name,Double price1,Double price2);

    /**根据多个条件组合动态查询。参数为null时不做查询条件*/
    @Query("select g from Goods g where (?1 = null or name like %?1%) and (?2 = null or price < ?2)")
    List<GoodsItem> findWithJPQLByNameContainingAndPrice(String name, Double price);

    /**使用SQL语句查询
     * 按照实际的数据库表名、字段写查询条件
     */
    @Query(value = "select * from Goods where name like ?1",nativeQuery = true)
    List<GoodsItem> findWithSQLByName(String name);

     /*根据name模糊查询，并分页*/
    Page<GoodsItem> findByNameContaining(String name, Pageable pageable);

}

